<?php
class Zend_Controller_Action_Helper_Item extends Zend_Controller_Action_Helper_Abstract
{
    public function viewNum($view, $itemid)
    {
        if (is_numeric($itemid))
        {
            $item = new CItem();
            if ($_COOKIE[COOKIE_PRE . 'product' . $itemid] != 1)
            {
                $item->addViewNum($itemid);
                setcookie(COOKIE_PRE . 'product' . $itemid, '1', time() + 3600, '/');
            }
            $view->assign('itemViewNum', $item->getViewNum($itemid));
        } else {
            $view->assign('itemViewNum', 0);
        }
    }

    public function getLagnappeArray()
    {
        $view = $this->getActionController()->view;
        $cart = new CCart();
        $sessionHash = $_COOKIE[COOKIE_PRE . 'product_buy_session'];
		$cookieString = $cart->getCartStr($sessionHash);
        if ($cookieString != '')
        {
            $shoppingArray = json_decode(stripslashes($cookieString), true);
        }

        $item = new CItem();
        $CLagnappe = new CLagnappe();

        $LagnappeArray = array();
        $fuidkeyArray = array();
        $totalPrice = 0;

        foreach ($shoppingArray as $key => $value)
        {
            $totalcounts = $shoppingArray[$key]['counts'];
            // echo($key.'--');
            // echo($item->getitemfuid($item->getitemid($key)).'--');
            foreach ($shoppingArray as $keya => $valuea)
            {
                if ($key && $keya)
                {
                    if ($key != $keya)
                    {
                        if ($item->getitemfuid($item->getitemid($key)) && $item->getitemfuid($item->getitemid($keya)) && $item->getitemfuid($item->getitemid($key)) == $item->getitemfuid($item->getitemid($keya)))
                        {
                            $totalcounts = $totalcounts + $shoppingArray[$keya]['counts'];
                        }
                    }
                }
            }
            $fuidkey = $item->getitemfuid($item->getitemid($key));
            if ($fuidkeyArray)
            {
                $k = array_search($fuidkey, $fuidkeyArray);
                if ($k === false)
                {
                    $LagnappeArraytmp = $CLagnappe->getItemsByItemid($fuidkey, $totalcounts);
                    if ($LagnappeArraytmp)
                    {
                        for ($i = 0; $i < count($LagnappeArraytmp); $i++)
                        {
                            array_unshift($LagnappeArray, $LagnappeArraytmp[$i]);
                        }
                    }
                }
            } else
            {
                $LagnappeArraytmp = $CLagnappe->getItemsByItemid($fuidkey, $totalcounts);
                if ($LagnappeArraytmp)
                {
                    for ($i = 0; $i < count($LagnappeArraytmp); $i++)
                    {
                        array_unshift($LagnappeArray, $LagnappeArraytmp[$i]);
                    }
                }
            }

            array_unshift($fuidkeyArray, $fuidkey);
        }
        $view->assign('LagnappeArray', $LagnappeArray);
        $attachment = new CLagnappeAttachment();
        $attachmentArray = array();
        for ($i = 0; $i < count($LagnappeArray); $i++)
        {
            $attachmentArray[$LagnappeArray[$i]["pid"]] = $attachment->getAttachments($LagnappeArray[$i]["pid"]);
        }

        $view->assign('attachmentArray', $attachmentArray);
        return array('LagnappeArray' => $LagnappeArray, 'attachmentArray' => $attachmentArray);
    }

    public function getOrderItemArray()
    {
        $view = $this->getActionController()->view;

        ini_set('memory_limit', '-1');

        $itemArray = array();

        $totalPrice = 0;

        //
        //echo $cookieString;
        if ( 1== 1) //判断登录未登录用户的cart
        {        	
        	$cart = new CCart(); 
        	$member = new CMember();
        	$productModel = new CProduct();   
        	if (!$member->isUser($this->getActionController()->getRequest()->getModuleName(),$this->getActionController()->getRequest()->getControllerName())||$_COOKIE[COOKIE_PRE.'member_type']!='buyer') {
        		$sessionHash = $_COOKIE[COOKIE_PRE . 'product_buy_session'];    	
        		$cartStr = $cart->getCartStr($sessionHash);
        	} else {
        		$cartStr = $cart->getCartStrByUid($_COOKIE[COOKIE_PRE.'uid']);
        	}
        	
        	if (!$cartStr) {        		
        		return '';
        	}
            $shoppingArray = json_decode(stripslashes($cartStr), true);
            $item = new CItem();
            $itemidAmountsArray = array();
			
            $price = new CProductPrice();
            foreach ($shoppingArray as $key => $value)
            {
            	$tmpProduct=$productModel->getDetail($key);
            	if($tmpProduct[0]['ispresale']==1){
            		$totalcounts=0;
            	}else{
            		$totalcounts = $shoppingArray[$key]['counts'];
            	}
                
                // echo($key.'--');
                // echo($item->getitemfuid($item->getitemid($key)).'--');
                foreach ($shoppingArray as $keya => $valuea)
                {
                    if ($key && $keya)
                    {
                        if ($key != $keya)
                        {
                            /*
                            if ($item->getitemfuid($item->getitemid($key)) && $item->getitemfuid($item->getitemid($keya)) && $item->getitemfuid($item->getitemid($key)) == $item->getitemfuid($item->getitemid($keya)))
                            {
                                $totalcounts = $totalcounts + $shoppingArray[$keya]['counts'];
                            }                     
                             */
                        	$tmpProducta=$productModel->getDetail($keya);
                        	if($tmpProducta[0]['ispresale']!=1){
                        		$totalcounts = $totalcounts + $shoppingArray[$keya]['counts'];
                        		
                        	}
                        }
                    }
                }
                $priceArray = $price->getAllPrice($key);
                
                
                // $itemArray_merge=$item->getItemsByItemid($key,$shoppingArray[$key]['pattern_name'],$shoppingArray[$key]['counts']);
                //$itemArray_merge = $item->getItemsByItemid($key, $shoppingArray[$key]['pattern_name'], $totalcounts);
                
                $itemArray_merge = $item->getItemsByItemid($key, $shoppingArray[$key]['pattern_name'], $totalcounts);
                //原始价格
                $itemArray_merge_org = $item->getItemsByItemid($key, $shoppingArray[$key]['pattern_name'], $value['counts']);
				$itemArray_merge[0]['net_price_org'] = $itemArray_merge_org[0]['net_price'];
				//用户存在该坑位
		        $uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
		        if ($uid) {
		        	$flashCommon = new CFlashcommon();
		        	if ($flashCommon->isUserFlashItem($uid, $key) || $flashCommon->isUserWhenEverFlashItem($uid, $key)) {
		        		$flashSaleCommon = new CFlashsales();
		        		$discount = $flashSaleCommon->getDiscount($key);
		        		$productAttr = new CProductattr();
		        		if ($productAttr->getAttr($key, 11)) {
				        	$i = 0;	
				        	        			
	        				if (count($result) == 4) {
		        				if ($i == 1) {
			        				$itemArray_merge_org[0]['net_price'] = 28.99;
					        		$itemArray_merge[0]['net_price_org'] = 28.99;
					        		$itemArray_merge[0]['net_price'] = 28.99;
		        				}
		        				if ($i == 2) {
		        					$itemArray_merge_org[0]['net_price'] = 25.99;
					        		$itemArray_merge[0]['net_price_org'] = 25.99;
					        		$itemArray_merge[0]['net_price'] = 25.99;
		        				}
		        				if ($i == 3) {
		        					$itemArray_merge_org[0]['net_price'] = 23.99;
					        		$itemArray_merge[0]['net_price_org'] = 23.99;
					        		$itemArray_merge[0]['net_price'] = 23.99;
		        				}
		        				
	        				}
		        		} elseif ($productAttr->getAttr($key, 12)) {
		        			$i = 0;		        			
	        				if (count($result) == 4) {
		        				if ($i == 1) {
			        				$itemArray_merge_org[0]['net_price'] = 33.99;
					        		$itemArray_merge[0]['net_price_org'] = 33.99;
					        		$itemArray_merge[0]['net_price'] = 33.99;
		        				}
		        				if ($i == 2) {
		        					$itemArray_merge_org[0]['net_price'] = 30.99;
					        		$itemArray_merge[0]['net_price_org'] = 30.99;
					        		$itemArray_merge[0]['net_price'] = 30.99;
		        				}
		        				if ($i == 3) {
		        					$itemArray_merge_org[0]['net_price'] = 28.99;
					        		$itemArray_merge[0]['net_price_org'] = 28.99;
					        		$itemArray_merge[0]['net_price'] = 28.99;
		        				}
	        				}
		        		} else {
			        		$itemArray_merge_org[0]['net_price'] = $priceArray[0]['net_price'];
			        		$itemArray_merge[0]['net_price_org'] = $priceArray[0]['net_price'];
			        		$itemArray_merge[0]['net_price'] = $priceArray[0]['net_price'];
		        		}
		        	}
		        }
		        
		        //预定用户购买
		        $preids = isset($_COOKIE[COOKIE_PRE.'preid']) ? $_COOKIE[COOKIE_PRE.'preid']:0;
		        if(isset($_COOKIE[COOKIE_PRE . 'uid']) && $_COOKIE[COOKIE_PRE . 'uid'] && $preids){
		        	$preorderModel = new CPreorder();
		        	$orderModel = new COrder();
		        	$productModel = new CProduct();
		        	$productDetail = $productModel->getDetail($key);
		        	//查看是否有预订单
		        	$preorderrow = $preorderModel->getCertainOrder($preids);
		        	
		        	if ($preorderrow && $preorderrow['itemid'] == $key && $_COOKIE[COOKIE_PRE . 'uid'] == $preorderrow['uid']) {
		        		$hasPay = 0;
		        		$orderRow = $orderModel->getDetail($preorderrow['orderid']);
			        	if (in_array($orderRow[0]['flid'], array('3','4', '5', '9'))) {
							$hasPay = 1;
						}
						if ($preorderrow['relate_orderid']) {
							$orderRow2 = $orderModel->getDetail($preorderrow['relate_orderid']);
							if (in_array($orderRow2[0]['flid'], array('1','7', '8'))) {
								$subPay = 0;
							} else {
								$subPay = 1;
							}
						} else {
							$subPay = 0;
						}
			        	if($productDetail[0]['ispresale']!=1&& $hasPay && !$subPay){			        		
			        		if(!empty($preorderrow)){			        		
		        				$Cpresale = new CProductpresale();
		        				$presalerow = $Cpresale->getPresaleproduct($key);
		        				$view->assign('presalerow', $presalerow);
		        				
		        				if(!empty($presalerow)&&$_COOKIE[COOKIE_PRE.'uid']==$preorderrow['uid']){
		        					$itemArray_merge_org[0]['net_price'] -= $presalerow[0]['preprice'];
		        					$itemArray_merge[0]['net_price_org'] -= $presalerow[0]['preprice'];
		        					$itemArray_merge[0]['net_price'] -= $presalerow[0]['preprice'];
		        				}			        			
			        		}
			        	}
		        	}			        
		        }
		        
		        
		        
		        
		        /*$presalerow = $presaleModel->getPresaleproduct($key);
		        if(!empty($presalerow)){
		        	$itemArray_merge_org[0]['net_price'] = $presalerow[0]['preprice'];
		        	$itemArray_merge[0]['net_price_org'] = $presalerow[0]['preprice'];
		        	$itemArray_merge[0]['net_price'] = $presalerow[0]['preprice'];
		        }*/
				
                $itemArray_merge[0]['min_counts'] = $priceArray[0]['min_counts'];

                $itemArray_merge[0]['max_counts'] = $priceArray[count($priceArray) - 1]['max_counts'];

                $itemArray_merge[0]['buy_pattern_size'] = $shoppingArray[$key]['pattern_size'];

                $itemArray_merge[0]['buy_BadgePatch'] = $shoppingArray[$key]['BadgePatch'];

                $itemArray_merge[0]['select_badgepatch'] = $shoppingArray[$key]['select_badgepatch'];
                $itemArray_merge[0]['badgepatchforsize'] = $shoppingArray[$key]['badgepatchforsize'];

                $itemArray_merge[0]['buy_prooriginalnumber'] = $shoppingArray[$key]['prooriginalnumber'];

                if ($shoppingArray[$key]['printAmounts'] && $shoppingArray[$key]['printAmounts'] != 0)
                {
                    $itemArray_merge[0]['buy_name_num'] = $shoppingArray[$key]['name_num'];                    

                    $itemArray_merge[0]['printAmounts'] = $shoppingArray[$key]['printAmounts'];
                }

                if ($shoppingArray[$key]['originalprintAmounts'] && $shoppingArray[$key]['originalprintAmounts'] != 0)
                {
                    $itemArray_merge[0]['buy_originalname_num'] = $shoppingArray[$key]['originalname_num'];

                    $itemArray_merge[0]['originalprintMesg'] = $shoppingArray[$key]['originalprintMesg'];

                    $itemArray_merge[0]['originalprintAmounts'] = $shoppingArray[$key]['originalprintAmounts'];                   
                }
                $itemArray_merge[0]['printMesg'] = $shoppingArray[$key]['printMesg'];
                $itemArray_merge[0]['accessory'] = $shoppingArray[$key]['accessory'];
                $itemArray_merge[0]['accessory_money']=0;
                if(!empty($shoppingArray[$key]['accessory_price'])){
                	foreach($shoppingArray[$key]['accessory_price'] as $k=>$v){
                		$itemArray_merge[0]['accessory_money']+=$v*$shoppingArray[$key]['quantity'][$k];
                	}
                }
                
                
                //$itemArray_merge[0]['accessory_price'] = $shoppingArray[$key]['accessory_price'];
                //$itemArray_merge[0]['accessory_size'] = $shoppingArray[$key]['accessory_size'];
                
                
                
                
                $itemArray_merge[0]['badge_patch_amount'] = $shoppingArray[$key]['badge_patch_amount'];	
                $itemArray_merge[0]['badge_patch_money'] = $shoppingArray[$key]['badge_patch_money'];
                array_unshift($itemArray, $itemArray_merge[0]);
                $itemidAmountsArray[$key . '-' . $shoppingArray[$key]['pattern_name']] = $shoppingArray[$key]['counts'];
            }
            

            foreach ($itemArray as &$value2)
            {
            	
                $value2['amounts'] = $itemidAmountsArray[$value2['itemid'] . '-' . $value2['pattern_name']];
            
            }
            foreach ($itemArray as $value3)
            {

                if ($value3['buy_name_num'] == '' and $value3['buy_originalname_num'] == '')
                {

                    $totalPrice+=$value3['amounts'] * $value3['net_price'];
                    $totalPriceOrg+=$value3['amounts'] * $value3['net_price_org'];
                } else
                {
                    $totalPrice+=$value3['amounts'] * $value3['net_price'] + $value3['originalprintAmounts'] * PRINT_ORIGINAL_PRICE + $value3['printAmounts'] * PRINT_CUSTOM_PRICE;
                    $totalPriceOrg+=$value3['amounts'] * $value3['net_price_org'] + $value3['originalprintAmounts'] * PRINT_ORIGINAL_PRICE + $value3['printAmounts'] * PRINT_CUSTOM_PRICE;
                }

                if ($value3['buy_BadgePatch'] == 'Yes')
                {
                    $totalPrice+=$value3['badge_patch_money'];  //臂章�?美元
                    $totalPriceOrg+=$value3['badge_patch_money'];
                    
                }
                
                	$totalPrice+=$value3['accessory_money'];  //臂章�?美元
                	$totalPriceOrg+=$value3['accessory_money'];  //臂章�?美元
                
                
            }

            $order = new COrder();
            $orderArray = $order->getAllOrderCounts($_COOKIE[COOKIE_PRE . 'uid'], "", "");
            if ($orderArray)
            {
                
            } else
            {
                $totalPrice = $totalPrice - SENT;
                $totalPriceOrg = $totalPriceOrg - SENT;
            }
        }	
        $mlm = new CMlm();
        $totalPriceDisSave = 0;
        if ($mlm->isMlmUserFirstBuy(isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid']:0)) {
        	
        	$config = $mlm->getConfig();
        	$tmp = $totalPrice;
        	$totalPrice = $totalPrice * $config['rebate']['first_user_discount'] / 100;
        	$totalPrice = $view->numberFormat($totalPrice, 2);
        	$totalPriceDisSave = $view->numberFormat($tmp - $totalPrice, 2);
        }
        
        $couponCodeUse = new CCouponCodeUse();
        $couponInfo = $couponCodeUse->getCodeInfo($_COOKIE[COOKIE_PRE.'uid'], $totalPrice, 0);
        if(!empty($couponInfo)){
        	$totalPrice -= $couponInfo['money'];
        }
        
        if(BLACK_ACTIVITY){
        	if($totalPrice>399.00){
        		//$totalPriceOrg-=100.00;
        		$totalPrice-=100.00;
        		$view->assign('black_friday', 1);
        	}
        }

        $view->assign('itemArray', $itemArray);
        $view->assign('couponInfo', $couponInfo);
        $view->assign('totalPrice', $totalPrice);
        $view->assign('totalPriceOrg', $totalPriceOrg);
       
        return array('itemArray' => $itemArray, 'totalPrice' => $totalPrice, 'totalPriceDisSave'=>$totalPriceDisSave, 'totalPriceOrg'=>$totalPriceOrg,'couponInfo'=>$couponInfo);
    }
}
